package sopt.dev.activity;

import static sopt.dev.util.SwipeListCreator.makeListToSwipeMode;

import java.util.List;

import sopt.dev.R;
import sopt.dev.data.CityAdatper;
import sopt.dev.data.CityVO;
import sopt.dev.data.GagebuDataModel;
import sopt.dev.data.ItemVO;
import sopt.dev.util.BackPressCloseHandler;
import sopt.dev.util.SharedPreferenceUtil;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class CityListActivity extends Activity implements OnItemClickListener,
		OnClickListener, OnItemLongClickListener {

	private CityAdatper mAdapter;
	private ListView cityList;
	private Button btnAdd;
	private Button btnSetting;
	private BackPressCloseHandler backPressCloseHandler = new BackPressCloseHandler(this);

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.city_list_activity);

		setAlertDialog();

		btnAdd = (Button) findViewById(R.id.city_list_btn_add);
		btnAdd.setOnClickListener(this);

		btnSetting = (Button) findViewById(R.id.city_list_btn_setting);
		btnSetting.setOnClickListener(this);

		cityList = (ListView) findViewById(R.id.city_list_listview);		
		cityList.setOnItemClickListener(this);
		cityList.setOnItemLongClickListener(this);
		

		mAdapter = new CityAdatper(this, GagebuDataModel.getInstance().readAllCityData());

		cityList.setAdapter(mAdapter);

		makeListToSwipeMode(mAdapter, cityList);

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.city_list_btn_add: {
			startActivity(new Intent(this, CityAddActivity.class));
			finish();
			break;
		}
		case R.id.city_list_btn_setting: {
			startActivity(new Intent(this, SettingActivity.class));
			
			break;

		}

		}
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		CityVO<ItemVO> data = (CityVO<ItemVO>) mAdapter.getItem(position);

		Intent i = new Intent(CityListActivity.this, CityDetailActivity.class);
		i.putExtra("city_data", data);
		startActivity(i);
	}

	// 각 아이템 longClick 시, CityAddActivity에 처음 입력했던 도시정보를 불러와서 수정가능하게 함
	@Override
	public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
			int position, long arg3) {
		CityVO<ItemVO> data = (CityVO<ItemVO>) mAdapter.getItem(position);

		Intent i = new Intent(CityListActivity.this, CityAddActivity.class);
		i.putExtra("city_data", data);
		i.putExtra("position", position);
		startActivity(i);

		return false;
	}

	public void setAlertDialog() {

		AlertDialog.Builder adb = new AlertDialog.Builder(this);
		LayoutInflater adbInflater = LayoutInflater.from(this);
		final SharedPreferenceUtil preference = SharedPreferenceUtil.getInstance(this);
	
		View eulaLayout = adbInflater.inflate(R.layout.alret_checkbox, null);
		final CheckBox dontShowAgain = (CheckBox) eulaLayout.findViewById(R.id.skip);
	
		adb.setView(eulaLayout);
		adb.setTitle("알림");
		adb.setMessage("ㅇㅇㅇ에서는 환율을 앱 작성 전날 기준 값으로 계산합니다. 결제 당시의 환율이나 각 카드사의 수수료 정책에 따라 실제 청구금액은 다를 수 있으니 참고해 주세요.");

		// 확인 버튼 클릭시
		adb.setPositiveButton("확인", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String checkBoxResult = "NOT checked";
				if (dontShowAgain.isChecked())
					checkBoxResult = "checked";
				preference.putSharedPreference("skipMessage", checkBoxResult);

				return;
			}
		});

		// 취소 버튼 클릭시
		adb.setNegativeButton("취소", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String checkBoxResult = "NOT checked";
				if (dontShowAgain.isChecked())
					checkBoxResult = "checked";
				preference.putSharedPreference("skipMessage", checkBoxResult);

				return;
			}
		});

		String skipMessage = preference
				.getStringSharedPreference("skipMessage");

		if (!skipMessage.equals("checked"))
			adb.show();

		super.onResume();
	}

	@Override
	public void onBackPressed() {	
		backPressCloseHandler.onBackPressed();
	}
}
